package com.appistry.storage.domain.impl;

import java.net.InetAddress;
import java.net.UnknownHostException;

import com.appistry.api.rest.domain.RestResponse;
import com.appistry.storage.Worker;
import com.appistry.storage.WorkerConfiguration;
import com.appistry.storage.WorkerInfo;
import com.appistry.storage.domain.AbstractRestDomainObject;
import com.appistry.storage.exception.StorageManagerException;
import com.appistry.storage.rest.RestStorageManager;
import com.appistry.storage.rest.domain.ControllerAction;
import com.appistry.storage.util.JsonTransformUtil;

public class WorkerImpl extends AbstractRestDomainObject implements Worker {
	private RestStorageManager manager;
	private String ip;
	private boolean isAvailable;
	private int port;

	public WorkerImpl(RestStorageManager manager, String ip) {
		this.manager = manager;
		this.ip = ip;
	}

	public InetAddress getInetAddress() {
		try {
			return InetAddress.getByName(ip);
		} catch (UnknownHostException e) {
			throw new RuntimeException(e);
		}
	}

	public String getIpAddress() {
		return ip;
	}

	public String getTerritory() throws StorageManagerException{
		try {
			RestResponse response = manager.getWorkerTerritory(getInetAddress());
			checkForErrors(response);

			return JsonTransformUtil.toTerritory(response.getBodyAsString());			
		}catch (StorageManagerException e) {
			throw e;
		} catch (Exception e) {
			throw new StorageManagerException(e);
		}
	}

	public WorkerConfiguration getWorkerConfiguration() throws StorageManagerException{
		try {
			RestResponse response = manager.getWorkerConfiguration(getInetAddress());
			checkForErrors(response);

			return JsonTransformUtil.toWorkerConfiguration(response.getBodyAsString());						
		}catch (StorageManagerException e) {
			throw e;
		} catch (Exception e) {
			throw new StorageManagerException(e);
		}
	}

	public WorkerInfo getWorkerInfo() throws StorageManagerException{
		try {
			RestResponse response = manager.getWorkerResource(getInetAddress());
			checkForErrors(response);

			return JsonTransformUtil.toWorkerInfo(response.getBodyAsString());
		}catch (StorageManagerException e) {
			throw e;
		} catch (Exception e) {
			throw new StorageManagerException(e);
		}
	}

	public void start() throws StorageManagerException {
		try {
			RestResponse response = manager.workerController(getInetAddress(), ControllerAction.START);
			checkForErrors(response);
		}catch (StorageManagerException e) {
			throw e;
		} catch (Exception e) {
			throw new StorageManagerException(e);
		}
	}

	public void stop() throws StorageManagerException {
		try {
			RestResponse response = manager.workerController(getInetAddress(), ControllerAction.STOP);
			checkForErrors(response);
		}catch (StorageManagerException e) {
			throw e;
		} catch (Exception e) {
			throw new StorageManagerException(e);
		}
	}

	public boolean isAvailable() {
		return isAvailable;
	}

	public int getPort() {
		return port;
	}

	public void setAvailable(boolean isAvailable) {
		this.isAvailable = isAvailable;
	}

	public void setPort(int port) {
		this.port = port;
	}

}
